﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace eBrain
{
    public partial class TransparentLayer : Form
    {
//        int curr_ID, min_ID, max_ID;
        //bool choice_performed = false;
        //bool bRedrawSelection = false;
        Color sel_color;
        int sel_size;
        RoiList myRL;

        public bool bSelectionHighlight = false;

        public AreaToHighlight SelectedArea;

        public TransparentLayer()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;

            SelectedArea = new AreaToHighlight();

            CheckAndMoveToSecondaryScreen();

            //TranspLayerForm.BackColor = System.Drawing.Color.GhostWhite;
            //TranspLayerForm.TransparencyKey = TranspLayerForm.BackColor;
            //TranspLayerForm.Width = myExpWin.Width;
            //TranspLayerForm.Height = myExpWin.Height;
            //TranspLayerForm.StartPosition = FormStartPosition.CenterParent;
        }

        /// <summary>
        /// Tratto da http://www.dotnetwatch.com/C-How-to-Show-windows-form-o444_AR.aspx
        /// </summary>
        public void CheckAndMoveToSecondaryScreen()
        {
            Screen[] sc;
            sc = Screen.AllScreens;

            if (sc.Length > 1)
            {
                this.Left = sc[1].WorkingArea.Width;
                this.Top = sc[1].WorkingArea.Height;
                this.StartPosition = FormStartPosition.Manual;
                this.Location = sc[1].Bounds.Location;

                //myLog.Log("Moving to the secondary screen" + "Left(& width)=" + this.Left + "; Top (& Height)=" + this.Top);
                Point p = new Point(sc[1].WorkingArea.Location.X, sc[0].WorkingArea.Location.Y);
                this.Location = p;

            }

            //            this.WindowState = FormWindowState.Maximized;
            //          this.Show();
        }

        public void Erase()
        {
            Graphics e = CreateGraphics();// Graphics.FromHwnd(hWnd_P3Speller);
            e.Clear(this.BackColor);
            e.Dispose();

            this.SendToBack();
        }


        public void UpdateSelectedArea(int ROI_ID)
        {
            //int _x, int _y, int _w, int _h, Color _c, int _size)//, int Persistence_ms)

            SelectedArea.x = myRL.myRoiList[ROI_ID].x0;
            SelectedArea.y = myRL.myRoiList[ROI_ID].y0;
            SelectedArea.w = myRL.myRoiList[ROI_ID].width;
            SelectedArea.h = myRL.myRoiList[ROI_ID].height;
            SelectedArea.pen_color = sel_color;
            SelectedArea.pen_size = sel_size;

            SelectedArea.toHighlight = true;

            this.Invalidate();
        }


        public void ManageHighlightSelection(RoiList RL, int ROI_selected, Color s_color, int size)
        {

            //int ret_int = 0;
            //int x0, y0, h, w;

            try
            {
                myRL = RL;
                sel_color = s_color;
                sel_size = size;

                //choice_performed = false;
                //curr_ID = ROI_selected;
                //min_ID = ROI_ID_min;
                //max_ID = ROI_ID_MAX;

                //porto il layer davanti
                this.TopMost = true;
                this.Show();
                this.BringToFront();
                this.WindowState = FormWindowState.Maximized;
                this.Activate();

                UpdateSelectedArea(ROI_selected);

                this.Invalidate();

                //bRedrawSelection = true;

                ////attivo la possibilità di scegliere altri target (OnKeyPress)
                ////this.KeyDown += new KeyEventHandler(OnKeyPress);

                //if (bRedrawSelection)
                //{
                //    //Assegno i valori alle variabili
                //    x0 = myRL.myRoiList[curr_ID].x0;
                //    y0 = myRL.myRoiList[curr_ID].y0;
                //    w = myRL.myRoiList[curr_ID].width;
                //    h = myRL.myRoiList[curr_ID].height;

                //    Graphics e = this.CreateGraphics();// Graphics.FromHwnd(hWnd_P3Speller);

                //    e.Clear(this.BackColor);

                //    e.DrawRectangle(new Pen(sel_color, size), x0, y0, w, h);

                //    e.Dispose();

                //    this.Refresh();
                //    Application.DoEvents();

                //    bRedrawSelection = false;

                //}

                //System.Media.SystemSounds.Asterisk.Play();

                if (false)
                {

                    //rimuovo la possibilità di scegliere altri target
                    //this.KeyDown -= OnKeyPress;

                    //riporto il form indietro
                    this.TopMost = false;
                    this.WindowState = FormWindowState.Normal;
                    this.Visible = false;
                    this.SendToBack();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("ManageHighlightSelection error : " + ex.Message + " - " + ex.Source.ToString());

            }
            //return curr_ID;
        }


        public void SetFrontWindow()
        {
            this.TopMost = true;
            //this.Show();
            this.BringToFront();
            this.WindowState = FormWindowState.Maximized;
            
            this.Activate();
            this.Focus();

            this.Refresh();
        }

        public void SetInBackground()
        {
            this.TopMost = false;
            this.WindowState = FormWindowState.Normal;
            this.SendToBack();
        }


        //public void UpdateSelHighlight()
        //{

        //    //Assegno i valori alle variabili
        //    int x0 = myRL.myRoiList[curr_ID].x0;
        //    int y0 = myRL.myRoiList[curr_ID].y0;
        //    int w = myRL.myRoiList[curr_ID].width;
        //    int h = myRL.myRoiList[curr_ID].height;

        //    Graphics e = this.CreateGraphics();// Graphics.FromHwnd(hWnd_P3Speller);

        //    e.Clear(this.BackColor);

        //    e.DrawRectangle(new Pen(sel_color, sel_size), x0, y0, w, h);

        //    e.Dispose();

        //    this.Refresh();

        //}


        //public int old_ManageHighlightSelection(RoiList myRL, int ROI_selected, int ROI_ID_min, int ROI_ID_MAX, Color sel_color, int size)
        //{
        //    //int ret_int = 0;
        //    int x0, y0, h, w;
        //    bool Loooppa = true;

        //    choice_performed = false;
        //    curr_ID = ROI_selected;
        //    min_ID = ROI_ID_min;
        //    max_ID = ROI_ID_MAX;

        //    //porto il layer davanti
        //    this.BringToFront();
        //    this.WindowState = FormWindowState.Maximized;
        //    this.TopMost = true;
        //    this.Activate();
        //    this.Show();

        //    bRedrawSelection = true;

        //    //attivo la possibilità di scegliere altri target (OnKeyPress)
        //    //this.KeyDown += new KeyEventHandler(OnKeyPress);


        //    //loop fino a che la selezione non è stata effettuata la selezione
        //    while (Loooppa)
        //    {

        //        if (bRedrawSelection)
        //        {
        //            //Assegno i valori alle variabili
        //            x0 = myRL.myRoiList[curr_ID].x0;
        //            y0 = myRL.myRoiList[curr_ID].y0;
        //            w = myRL.myRoiList[curr_ID].width;
        //            h = myRL.myRoiList[curr_ID].height;

        //            Graphics e = this.CreateGraphics();// Graphics.FromHwnd(hWnd_P3Speller);

        //            e.Clear(this.BackColor);

        //            e.DrawRectangle(new Pen(sel_color, size), x0, y0, w, h);

        //            e.Dispose();

        //            this.Invalidate();
        //            Application.DoEvents();

        //            bRedrawSelection = false;

        //            if (choice_performed)
        //                Loooppa = false;

        //        }

        //        Thread.Sleep(100);
        //        Application.DoEvents();

        //    }

        //    this.Refresh();

        //    //rimuovo la possibilità di scegliere altri target
        //    //this.KeyDown -= OnKeyPress;

        //    //riporto il form indietro
        //    this.TopMost = false;
        //    this.WindowState = FormWindowState.Normal;
        //    this.Visible = false;
        //    this.SendToBack();

        //    return curr_ID;
        //}

        protected override void OnPaint(PaintEventArgs e)
        {
            //Console.WriteLine("OnPaint");

            if (SelectedArea.toHighlight)
                e.Graphics.DrawRectangle(new Pen(SelectedArea.pen_color, SelectedArea.pen_size),
                    SelectedArea.x, SelectedArea.y, SelectedArea.w, SelectedArea.h);

            base.OnPaint(e);

        }


        //public void OnKeyDown(object sender, KeyEventArgs e)
        //{
        //    System.Media.SystemSounds.Exclamation.Play();

        //    switch (e.KeyCode)
        //    {
        //        case Keys.Left:
        //            if (--curr_ID < min_ID)
        //                curr_ID = max_ID;
        //            break;

        //        case Keys.Right:
        //            if (++curr_ID > max_ID)
        //                curr_ID = min_ID;

        //            break;

        //        case Keys.Enter:

        //            choice_performed = true;

        //            break;
        //    }

        //    // UpdateSelHighlight();
        //    // this.Update();
        //    UpdateSelectedArea(curr_ID);
        //    this.Invalidate();
        //    //bRedrawSelection = true;
        //    //this.Refresh();
        //}


        public void UpdateP3WinSelectedArea_Step1(int x, int y, int w, int h, Color c, int size)
        {
            this.BringToFront();
            this.WindowState = FormWindowState.Maximized;
            this.TopMost = true;
            this.Show();

            Graphics e = this.CreateGraphics();// Graphics.FromHwnd(hWnd_P3Speller);

            e.Clear(this.BackColor);
            this.Update();

            e.DrawRectangle(new Pen(c, size), x, y, w, h);
            //e.DrawRectangle(new Pen(Color.Green, 10), 0, 0, 100, 100);
            //this.Refresh();

            Thread.Sleep(500);

            e.Dispose();
            //this.Refresh();
        }

        public void HideLayer_Step2()
        {
            this.TopMost = false;
            this.SendToBack();
            this.Refresh();
        }


        public void UpdateP3WinSelectedArea(int x, int y, int w, int h, Color c, int size, int showtime_ms)
        {
            this.Show();
            this.BringToFront();
            this.WindowState = FormWindowState.Maximized;
            this.TopMost = true;

            this.Refresh();

            Graphics e = this.CreateGraphics();// Graphics.FromHwnd(hWnd_P3Speller);

            e.DrawRectangle(new Pen(c, size), x, y, w, h);
            //e.DrawRectangle(new Pen(Color.Green, 10), 0, 0, 100, 100);

            e.Dispose();

            Thread.Sleep(showtime_ms);

            this.TopMost = false;
            this.SendToBack();
            this.Refresh();
        }

        //private void TransparentLayer_Click(object sender, EventArgs e)
        //{
        //    //UpdateSelHighlight();
        //    this.Update();
        //    System.Media.SystemSounds.Asterisk.Play();
        //}

        //private void TransparentLayer_DoubleClick(object sender, EventArgs e)
        //{
        //    System.Media.SystemSounds.Exclamation.Play();
        //}

        //private void TransparentLayer_KeyDown(object sender, KeyEventArgs e)
        //{
        //    switch (e.KeyCode)
        //    {
        //        case Keys.Left:
        //            if (--curr_ID < min_ID)
        //                curr_ID = max_ID;
        //            break;

        //        case Keys.Right:
        //            if (++curr_ID > max_ID)
        //                curr_ID = min_ID;

        //            break;

        //        case Keys.Enter:

        //            choice_performed = true;

        //            break;
        //    }

        //    // UpdateSelHighlight();
        //    // this.Update();
        //    this.Invalidate();

        //    bRedrawSelection = true;
        //    //this.Refresh();
        //}


    }
}
